package ajax;

import pojo.hibernate.Subinstitutionmaster;
import pojo.hibernate.SubinstitutionmasterDAO;

import pojo.hibernate.Departmentmaster;
import pojo.hibernate.DepartmentmasterDAO;

import pojo.hibernate.Institutionuserroles;
import pojo.hibernate.InstitutionuserroleDAO;

import pojo.hibernate.Institutionmaster;
import pojo.hibernate.InstitutionmasterDAO;

import pojo.hibernate.ErpmItemCategoryMaster;
import pojo.hibernate.ErpmItemCategoryMasterDao;

import pojo.hibernate.ErpmCapitalCategory;
import pojo.hibernate.ErpmCapitalCategoryDao;

import pojo.hibernate.Statemaster;
import pojo.hibernate.StatemasterDAO;

import pojo.hibernate.Erpmusers;
import pojo.hibernate.ErpmusersDAO;


import utils.DevelopmentSupport;
import com.opensymphony.xwork2.Action;

import java.io.PrintWriter;
import java.util.List;

public class AjaxAction extends DevelopmentSupport {

    private String outstr = "";
    private String searchValue;
    private String searchValue2;


    public String getOutstr() {
        return outstr;
    }

    public void setOutstr(String outstr) {
        this.outstr = outstr;
    }

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public String getSearchValue2() {
        return searchValue2;
    }

    public void setSearchValue2(String searchValue2) {
        this.searchValue2 = searchValue2;
    }


    public String getSubinstitutionList() throws Exception {
        PrintWriter out = getResponse().getWriter();
        if (searchValue.length() > 0) {
      
            List<Subinstitutionmaster> simList = null;
            SubinstitutionmasterDAO simDao = new SubinstitutionmasterDAO();
            try {
                simList = simDao.findBysimImId(Short.parseShort(searchValue));

                if (simList != null && !simList.isEmpty()) {
                    for (Subinstitutionmaster sim : simList) {
                        Integer val = sim.getSimId();
                        String str = sim.getSimName();

                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } 
            out.print(outstr);
            out.flush();
            out.close();

        return Action.SUCCESS;
    }

    public String getDepartmentList() throws Exception {
        PrintWriter out = getResponse().getWriter();
               
        if (searchValue.length() > 0) {
            List<Departmentmaster> dmList = null;
            DepartmentmasterDAO dmDao = new DepartmentmasterDAO();
            try {
                dmList = dmDao.findBydmSimId(Integer.parseInt(searchValue));                
                if (dmList != null && !dmList.isEmpty()) {
                    for (Departmentmaster dm : dmList) {
                        Integer val = dm.getDmId();
                        String str = dm.getDmName();
                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
            out.print(outstr);
            out.flush();
            out.close();
        return Action.SUCCESS;
    }

    public String getInstitutionUserRoleList() throws Exception {
        PrintWriter out = getResponse().getWriter();

        if (searchValue.length() > 0) {
            List<Institutionuserroles> iurList = null;
            InstitutionuserroleDAO iurDao = new InstitutionuserroleDAO();
            try {
                iurList = iurDao.findByInstitutionId(Short.parseShort(searchValue));
                if (iurList != null && !iurList.isEmpty()) {
                    for (Institutionuserroles iur : iurList) {
                        Integer val = iur.getIurId();
                        String str = iur.getIurName();
                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }                
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            out.print(outstr);
            out.flush();
            out.close();
        }
        return Action.SUCCESS;
    }

    public String getInstitutions() throws Exception {
        PrintWriter out = getResponse().getWriter();

            List<Institutionmaster> imList = null;
            InstitutionmasterDAO imDao = new InstitutionmasterDAO();
            try {
                imList = imDao.findAll();
                if (imList != null && !imList.isEmpty()) {
                    for (Institutionmaster im : imList) {
                        Short val = im.getImId();
                        String str = im.getImName();
                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            out.print(outstr);
            out.flush();
            out.close();
        return Action.SUCCESS;
    }

public String getSubCategory1List() throws Exception {
       PrintWriter out = getResponse().getWriter();

        if (searchValue.length() > 0) {
            List<ErpmItemCategoryMaster> erpmIcmList = null;
            ErpmItemCategoryMasterDao erpmIcmDao = new ErpmItemCategoryMasterDao();
            try {
                erpmIcmList = erpmIcmDao.findByerpmItemCategoryMaster(Integer.parseInt(searchValue));
                if (erpmIcmList != null && !erpmIcmList.isEmpty()) {
                    for (ErpmItemCategoryMaster erpmIcm : erpmIcmList) {
                        Integer val = erpmIcm.getErpmicmItemId();
                        String str = erpmIcm.getErpmicmCatDesc();
                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            out.print(outstr);
            out.flush();
            out.close();
        }
        return Action.SUCCESS;
    }

public String CapitalCategoryList() throws Exception {
       PrintWriter out = getResponse().getWriter();

        if (searchValue.length() > 0) {
            List<ErpmCapitalCategory> erpmccList = null;
            ErpmCapitalCategoryDao erpmccDao = new ErpmCapitalCategoryDao();
            try {                
                erpmccList = erpmccDao.findByImId(Short.parseShort(searchValue));
                if (erpmccList != null && !erpmccList.isEmpty()) {
                    for (ErpmCapitalCategory erpmIcc : erpmccList) {
                        Integer val = erpmIcc.getErpmccId();
                        String str = erpmIcc.getErmccDesc();
                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
            out.print(outstr);
            out.flush();
            out.close();

        return Action.SUCCESS;
    }


public String getStateList() throws Exception {
       PrintWriter out = getResponse().getWriter();
       
        if (searchValue.length() > 0) {
            List<Statemaster> smList = null;
            StatemasterDAO smDao = new StatemasterDAO();
            try {
                smList = smDao.findByCountryId(Byte.parseByte(searchValue));
                if (smList != null && !smList.isEmpty()) {
                    for (Statemaster smIcc : smList) {
                        Integer val = Integer.parseInt(smIcc.getStateId().toString());
                        String str = smIcc.getStateName();
                        if (outstr.length() == 0) {
                            outstr = val + "|" + str;
                        } else {
                            outstr += "," + val + "|" + str;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
            out.print(outstr);
            out.flush();
            out.close();

        return Action.SUCCESS;
    }


public String getSecretQuestion() throws Exception {
       PrintWriter out = getResponse().getWriter();

       //searchValue contains user name; searchValue2 contains user's date of birth

            outstr = "Invalid user name or date of birth";
            try {
                ErpmusersDAO userDao = new  ErpmusersDAO();
                outstr = userDao.RetrieveSecretQuestion(searchValue, searchValue2);

            } catch (Exception e) {
                e.printStackTrace();
            }
            out.print(outstr);
            out.flush();
            out.close();

        return Action.SUCCESS;
    }


}
